<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cart;
use APP\Models\Goods;
use App\Models\GoodsNorm;
use App\Models\User;
use App\Http\Controllers\App\BaseController;
use App\Models\Shop;

class CartController extends BaseController
{
    /**
     * 添加购物车
     *
     * @return void
     */
    public function create( Request $request )
    {   
        $user_id       = session('user_id');
    
        $goods_id      = $request->input('good_id');
        $goods_norm_id = $request->input('goods_norm_id');
        $num           = $request->input('num');

        $goods_ids = GoodsNorm::where('goods_id', $goods_id)->pluck('id')->toArray();
       
        if ( !in_array( $goods_norm_id, $goods_ids)) {
            return self::error('请传递正确的规格');
        }


        $info = Cart::firstOrNew([
                    'user_id'       => $user_id,
                    'goods_id'      => $goods_id,
                    'goods_norm_id' => $goods_norm_id,
                    'status'        => 0,
                ]);
        $info->num = $info->num + $num;
        
        $info->save();

        return self::success('添加购物车成功');
    }

    /**
     * 展示购物车
     *
     * @return void
     */
    public function show()
    {   
        $user_id = session('user_id');
      
        $where = [
            ['user_id', '=', $user_id],
            ['status', '=', 0],
        ];

        $data = Cart::with([
            'goods' => function ( $query ) {
                $query->select('name', 'pic', 'retail_price', 'id');
            },
            'norm' => function ( $query ) {
                $query->select('name', 'id');
            }
        ])->where($where)->get();

        $data = Cart::show_cart( $data );

        return self::json($data);
    }

    /**
     * 删除购物车商品
     *
     * @return void
     */
    public function del( Request $request )
    {
        $id      = $request->input('id');
   
        $user_id = session('user_id');
      
        $info = Cart::where('user_id', $user_id)->where('id', $id)->where('status', 0)->first();

        if ( !$info ) {
            return self::error('请删除正确的订单');
        }
       
        $info->delete();
       

        return self::success('删除成功');
    }

    /**
     * 修改商品数量
     *
     * @return void
     */
    public function edit( Request $request )
    {
        $user_id = session('user_id');
        
        $id   = $request->input('id');
        $num  = (int)$request->input('num');

        if ( $num < 1 ) {
            return self::error('商品数量最少为1');
        }
        $info = Cart::where('user_id', $user_id)->where('id', $id)->first();

        if ( !$info ) {
                return self::error('请删除正确的订单');
        }
   
        $info->num = $num;
        $info->save();
        
        return self::success('');
    }

    /**
     * 获取提货信息
     *
     * @param Request $request
     * @return void
     */
    public function info( Request $request )
    {
        $user_id = session('user_id');
        
        $info = User::select('shop_id', 'id', 'real_name', 'user_mobile')->find($user_id)->toArray();
        
        $shop_info = Shop::where('status', 1)->select('province', 'city', 'area', 'address')->find($info['shop_id']);

        if ( !$shop_info ) {
             $shop_info = Shop::where('login', '80000')->select('province', 'city', 'area', 'address')->first();
        }

        $shop_info = $shop_info->toArray();  

        $data = $info + $shop_info;
         
        return self::json($data);
    }


    
}

